package org.hj.modules.act.procdef.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.hj.common.api.vo.Result;
import org.hj.modules.AcBaseController;
import org.hj.modules.act.procdef.service.ProcdefService;
import org.hj.modules.act.procdef.service.RuprocdefService;
import org.hj.modules.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 说明：流程管理
 *   作者：Joson 
 */
@Controller
@RequestMapping("/act/procdef")
public class ProcdefController extends AcBaseController {
	
	@Autowired
	private ProcdefService procdefService;
	
	@Autowired
	private RuprocdefService ruprocdefService;
	
	/**列表
	 * @param page
	 * @throws Exception
	 */
	@RequestMapping(value="/list")
	@ResponseBody
	public Result<IPage> list(@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
							  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize, HttpServletRequest req)  throws Exception{

		Result<IPage> result = new Result<>();

		IPage page =new Page(pageNo, pageSize);
		String KEYWORDS=req.getParameter("KEYWORDS");

		List<PageData> dataList = procdefService.getpage(KEYWORDS,pageNo,pageSize);
		int count =procdefService.getTotal(KEYWORDS);
		page.setRecords(dataList);
		page.setTotal(count);
		result.setResult(page);
		return result;

		/*Map<String,Object> map = new HashMap<String,Object>();
		String errInfo = "success";
		PageData pd =  new PageData(req);
		String KEYWORDS = pd.getString("KEYWORDS");						//关键词检索条件
		if(Tools.notEmpty(KEYWORDS))pd.put("keywords", KEYWORDS.trim());
		String STRARTTIME = pd.getString("STRARTTIME");					//开始时间
		String ENDTIME = pd.getString("ENDTIME");						//结束时间
		if(Tools.notEmpty(STRARTTIME))pd.put("lastStart", STRARTTIME+" 00:00:00");
		if(Tools.notEmpty(ENDTIME))pd.put("lastEnd", ENDTIME+" 00:00:00");

		List<PageData> varList = procdefService.list(page);
		map.put("varList", varList);
		map.put("page", page);
		map.put("result", errInfo);				//返回结果
		return map;*/
	}
	
	/**获取预览XML
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/getViewXml")
	@ResponseBody
	public Object getViewXml(HttpServletRequest req)throws Exception{
		Map<String,Object> map = new HashMap<String,Object>();
		String errInfo = "success";
		PageData pd =  new PageData(req);
		String DEPLOYMENT_ID_ = pd.getString("DEPLOYMENT_ID_");		//部署ID
		createXmlAndPng(DEPLOYMENT_ID_);							//生成XML和PNG
		String code = FileUtil.readFileAllContent(Const.FILEACTIVITI+URLDecoder.decode(pd.getString("FILENAME"), "UTF-8"));
		map.put("code", code);
		map.put("result", errInfo);				//返回结果
		return map;
	}
	
	/**获取预览PNG
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/getViewPng")
	@ResponseBody
	public Object getViewPng(HttpServletRequest req)throws Exception{
		Map<String,Object> map = new HashMap<String,Object>();
		String errInfo = "success";
		PageData pd =  new PageData(req);
		String DEPLOYMENT_ID_ = pd.getString("DEPLOYMENT_ID_");		//部署ID
		createXmlAndPng(DEPLOYMENT_ID_);							//生成XML和PNG
		String FILENAME = URLDecoder.decode(pd.getString("FILENAME"), "UTF-8");
		pd.put("FILENAME", FILENAME);
		String imgSrcPath = PathUtil.getProjectpath()+Const.FILEACTIVITI+FILENAME;
		map.put("imgSrc", "data:image/jpeg;base64,"+ImageAnd64Binary.getImageStr(imgSrcPath)); //解决图片src中文乱码，把图片转成base64格式显示(这样就不用修改tomcat的配置了)
		map.put("result", errInfo);				//返回结果
		return map;
	}
	
	/**打包下载xml和png
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value="/download")
	public void download(HttpServletRequest req,HttpServletResponse response)throws Exception{
		PageData pd =  new PageData(req);
		String DEPLOYMENT_ID_ = pd.getString("DEPLOYMENT_ID_");		//部署ID
		createXmlAndPng(DEPLOYMENT_ID_);							//生成XML和PNG
		/*生成的全部代码压缩成zip文件*/
		if(FileZip.zip(PathUtil.getProjectpath()+"uploadFiles/activitiFile", PathUtil.getProjectpath()+"uploadFiles/activitiFile.zip")){
			/*下载代码*/
			FileDownload.fileDownload(response, PathUtil.getProjectpath()+"uploadFiles/activitiFile.zip", "activitiFile.zip");
		}
	}
	
	/**激活or挂起流程实例
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/onoffPro")
	@ResponseBody
	public Object onoffProcessDefinition(HttpServletRequest req)throws Exception{
		Map<String,Object> map = new HashMap<String,Object>();
		String errInfo = "success";
		int STATUS = Integer.parseInt(req.getParameter("STATUS").toString());
		String ID_ = req.getParameter("ID_");
		if(STATUS == 2){
			//pd.put("STATUS", 2);				//激活前先把此流程的所有任务状态设置成挂起状态
			ruprocdefService.onoffAllTask(2,ID_);
			activateProcessDefinitionById(ID_);	//激活流程实例
			map.put("message", "流程已被激活");
		}else{
			//挂起前先把此流程的所有任务状态设置成激活状态
			ruprocdefService.onoffAllTask(1,ID_);
			suspendProcessDefinitionById(ID_);	//挂起流程实例
			map.put("message", "流程已被挂起");

		}
		map.put("result", errInfo);				//返回结果
		return map;
	}
	
	/**导入流程
	 * @param file
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/uploadPro")
	@ResponseBody
	public Object uploadPro(@RequestParam(value="zip",required=false) MultipartFile file){
		Map<String,Object> map = new HashMap<String,Object>();
		String errInfo = "success";
		if (null != file && !file.isEmpty()) {
			String filePath = PathUtil.getProjectpath() + Const.FILEACTIVITI;		//文件上传路径
			String fileName =  FileUpload.fileUp(file, filePath, "proFile");		//执行上传
			try {
				deploymentProcessDefinitionFromZip("FHPRO", filePath+fileName);
			} catch (Exception e) {
				errInfo = "errer";
			}
		}
		map.put("result", errInfo);				//返回结果
		return map;
	}
	
	/**删除
	 * @param out
	 * @throws Exception
	 */
	@RequestMapping(value="/delete")
	@ResponseBody
	public Object delete(HttpServletRequest req) throws Exception{
		Map<String,String> map = new HashMap<String,String>();
		PageData pd =  new PageData(req);
		String DEPLOYMENT_ID_ = pd.getString("DEPLOYMENT_ID_");		  //部署ID
		deleteDeployment(DEPLOYMENT_ID_);
		map.put("result", "success");
		return map;
	}
	
	 /**批量删除
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/deleteAll")
	@ResponseBody
	public Object deleteAll(HttpServletRequest req) throws Exception{
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd =  new PageData(req);
		String errInfo = "success";
		String DATA_IDS = pd.getString("DATA_IDS");
		if(Tools.notEmpty(DATA_IDS)){
			String ArrayDATA_IDS[] = DATA_IDS.split(",");
			for(int i=0;i<ArrayDATA_IDS.length;i++){
				deleteDeployment(ArrayDATA_IDS[i]);
			}
		}else{
			errInfo = "error";
		}
		map.put("result", errInfo);				//返回结果
		return map;
	}

}
